import { defineStore } from 'pinia'
import request from '@/utils/request'
import { IApiRes, BannerItem, GoodItem, HotGoods } from '@/types/data'

export default defineStore('home', {
  state: () => {
    return {
      bannerList: [] as BannerItem[],  // 轮播图
      newGoodList: [] as GoodItem[],  // 新鲜好物
      hotGoodList: [] as HotGoods[]  // 人气推荐
    }
  },
  actions: {
    // 获取轮播图数据
    async getBannerList() {
      const res = await request.get<IApiRes<BannerItem[]>>('/home/banner')
      // console.log(res)
      this.bannerList = res.data.result
    },
    // 获取新鲜好物
    async getNewList() {
      const res = await request.get<IApiRes<GoodItem[]>>('/home/new')
      // console.log(res)
      this.newGoodList = res.data.result
    },
    // 人气推荐
    async getHotList() {
      const res = await request.get<IApiRes<HotGoods[]>>('/home/hot')
      // console.log(res)
      this.hotGoodList = res.data.result
    }
  }
})